package com.game.yxy.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.game.yxy.common.Result;
import com.game.yxy.entity.CusFile;
import com.game.yxy.entity.CusQ;
import com.game.yxy.mapper.CusQMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/cusq")
@Api(tags = "客户问题管理") // 描述整个控制器的作用
public class CusQController {
    @Resource
    CusQMapper cusqMapper;

    // 添加客户问题
    @PostMapping
    @ApiOperation(value = "添加客户问题", notes = "创建一个新的客户问题记录")
    public Result<?> save(@RequestBody CusQ cusq) {
        cusq.setTime(new Date()); // 服务端设置时间
        cusqMapper.insert(cusq);
        return Result.success();
    }

    // 更新客户问题
    @PutMapping
    @ApiOperation(value = "更新客户问题", notes = "根据ID更新客户问题记录")
    public Result<?> update(@RequestBody CusQ cusq) {
        cusqMapper.updateById(cusq);
        return Result.success();
    }

    // 删除客户问题
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除客户问题", notes = "根据ID删除客户问题记录")
    public Result<?> delete(@ApiParam(value = "客户问题ID", required = true) @PathVariable Long id) {
        cusqMapper.deleteById(id);
        return Result.success();
    }

    // 查询客户问题
    @GetMapping
    @ApiOperation(value = "查询客户问题", notes = "支持姓名、关键字查询")
    public Result<?> findPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "50") Integer pageSize,
            @ApiParam(value = "姓名", defaultValue = "") @RequestParam(defaultValue = "") String name,
            @ApiParam(value = "搜索关键词", defaultValue = "") @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<CusQ> wrapper = Wrappers.<CusQ>lambdaQuery();
        if (StrUtil.isNotBlank(name)) {
            wrapper.like(CusQ::getName, name);
        }
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(CusQ::getQue, search);
        }
        Page<CusQ> CusQPage = cusqMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(CusQPage);
    }
    
    // 根据ID查询客户问题
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询客户问题", notes = "根据客户问题ID查询客户问题记录")
    public Result<?> findById(@ApiParam(value = "客户问题ID", required = true) @PathVariable Long id){
        CusQ cusQ = cusqMapper.selectById(id);
        if (cusQ != null) {
            return Result.success(cusQ);
        } else {
            return Result.failure("客户问题不存在");
        }
    }
}